package com.sgb.kjwl.viewmodel.transport;

import android.text.TextUtils;
import android.util.Log;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.GasListInfoEntity;
import com.sgb.kjwl.model.entity.transport.GasFilterEntity;
import com.sgb.kjwl.model.entity.transport.GoodsInfoTypeEntity3;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.TransportRepertory;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.activity.transport.GasServiceActivity;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class GasServiceModule {
    private TransportRepertory repertory;
    private GasServiceActivity activity;
    public GasFilterEntity infoEntity=new GasFilterEntity();
    int page=1;

    public GasServiceModule(TransportRepertory repertory, GasServiceActivity activity) {
        this.repertory = repertory;
        this.activity = activity;
    }

    public void getFilter(boolean isMore){
        page=isMore?page+1:1;
        infoEntity.setPage(page);
        infoEntity.setLimit(10);
        Map map=new HashMap();
        map.put("page",page);
        map.put("limit",10);
        map.put("longitude",infoEntity.getLongitude());
        map.put("latitude",infoEntity.getLatitude());
       // map.put("areacode",repertory.getAreaCode());
        if(!TextUtils.isEmpty(infoEntity.getCityNo())){
            map.put("cityNo",infoEntity.getCityNo());
        }
        if(!TextUtils.isEmpty(infoEntity.getOilTypeNo())){
            map.put("oilTypeNo",infoEntity.getOilTypeNo());
        }
        if(infoEntity.getBrandNos()!=null&&infoEntity.getBrandNos().size()>0){
            map.put("brandNos",infoEntity.getBrandNos());
        }
        if(infoEntity.getPriceSort()==1){//1价格优先 2距离优先
            map.put("appOrder",1);
        }else{
            map.put("appOrder",2);
        }

       // infoEntity.setAreacode(repertory.getAreaCode());
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getSelfOwnerAPI().getGasList(map).enqueue(new Callback<BaseEntity<ListResponse<List<GasListInfoEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<GasListInfoEntity>>>> call, Response<BaseEntity<ListResponse<List<GasListInfoEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.getList(response.body().getData().getList(),isMore);


                }else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<GasListInfoEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });



    }
    public void getPriceType(){
        ArrayList<GoodsInfoTypeEntity3> goodsInfoTypeEntity3s=new ArrayList<>();
        GoodsInfoTypeEntity3 goodsInfoTypeEntity3=new GoodsInfoTypeEntity3();
        goodsInfoTypeEntity3.setName("价格优先");
        GoodsInfoTypeEntity3 goodsInfoTypeEntity=new GoodsInfoTypeEntity3();
        goodsInfoTypeEntity.setName("距离优先");
        goodsInfoTypeEntity3s.add(goodsInfoTypeEntity3);
        goodsInfoTypeEntity3s.add(goodsInfoTypeEntity);
        activity.setType(goodsInfoTypeEntity3s);
    }

    public void oilType() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getSelfOwnerAPI().oilTypePop().enqueue(new Callback<BaseEntity<List<GoodsInfoTypeEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Response<BaseEntity<List<GoodsInfoTypeEntity3>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.setTypePop(response.body().getData());
                } else {
                    MToast.showToast(activity.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void oilBrand() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getSelfOwnerAPI().getBrandPop().enqueue(new Callback<BaseEntity<List<GoodsInfoTypeEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Response<BaseEntity<List<GoodsInfoTypeEntity3>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.setBrandPop(response.body().getData());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getAddressModel() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getGoodsAPI().getCityList().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(APIManager.RESPONSE_OK))
                    activity.initAddressData(response.body().getData());

            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
            }
        });
    }
    // 详情
    public void getH5Url(String no,String oilModelNo) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getSelfOwnerAPI().getUrlSetting("gasStationDetails").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                Log.e("madong_test","3-------------"+response.body().getData().getUrl()+"       "+response.body().getData());
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?no=" + no+"&oilModelNo="+oilModelNo;
                    Log.e("madong_test","4====="+url);
                    showUrl(url);
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    private void showUrl(String url) {
        WebViewX5HostActivity.start(activity, url);
    }
}
